package com.status200.service;

import com.status200.model.Tutor;
import com.status200.repository.TeacherRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 导师管理的服务
 *
 * @author status200
 * @date 2017/12/17
 */
@Service
public class TeacherService {

    private final TeacherRepository teacherRepository;

    public TeacherService(TeacherRepository teacherRepository) {
        this.teacherRepository = teacherRepository;
    }

    /**
     * 添加导师。
     * Transactional注解是设置事务相关的注解。
     * @return
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public List<Tutor> getAllTutor() {
        return teacherRepository.findAll();
    }

    /**
     * 更新导师
     * @param tutor
     */
    @Transactional(isolation = Isolation.READ_COMMITTED,rollbackFor = Exception.class)
    public void updateTutor(Tutor tutor) {
        teacherRepository.save(tutor);
    }

    /**
     * 删除导师
     * @param tutor
     */
    @Transactional(isolation = Isolation.READ_COMMITTED,rollbackFor = Exception.class)
    public void deleteTutor(Tutor tutor) {
        teacherRepository.delete(tutor);
    }

    /**
     * 添加导师
     * @param tutor
     */
    @Transactional(isolation = Isolation.READ_COMMITTED,rollbackFor = Exception.class)
    public void addTutor(Tutor tutor) {
        teacherRepository.save(tutor);
    }
}
